package com.gitee.feizns.upload.controller;

import com.gitee.feizns.ArrayUtils;
import com.gitee.feizns.upload.config.interceptors.TokenValidate;
import com.gitee.feizns.upload.domain.ao.ResourceAo;
import com.gitee.feizns.upload.domain.ao.ResourceMAo;
import com.gitee.feizns.upload.domain.enums.ResourceType;
import com.gitee.feizns.upload.domain.error.Code;
import com.gitee.feizns.upload.domain.prop.AuthorizationConfigProperties;
import com.gitee.feizns.upload.domain.vo.ResourceVo;
import com.gitee.feizns.upload.service.UploadService;
import com.gitee.feizns.web.domain.result.Ret;
import com.gitee.feizns.web.ex.HttpException;
import com.gitee.feizns.web.ex.ServiceException;
import com.gitee.feizns.web.utils.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author feizns
 * @since 2019/8/9
 */
@RestController
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 文件上传
     * @param ao
     * @return
     */
    @PostMapping
    @TokenValidate
    public Ret<ResourceVo> upload(@Validated ResourceAo ao) {
        if ( ao.getFile() == null || ao.getFile().isEmpty() )
            throw new HttpException(HttpStatus.BAD_REQUEST, Code.FILE_CANNOT_BE_EMPTY);

        return Ret.ok(upload(ao.getFile(), ao.getType()));
    }

    @PostMapping("/m")
    public Ret<List<ResourceVo>> upload(@Validated ResourceMAo ao) {

        //验证
        allValidate(ao.getFiles());

        return Ret.ok(
                Arrays.stream(ao.getFiles())
                .map(file -> upload(file, ao.getType()))
                .collect(Collectors.toList())
        );
    }

    private ResourceVo upload(MultipartFile file, ResourceType type) {
        if ( file == null || file.isEmpty() )
            throw new HttpException(HttpStatus.BAD_REQUEST, Code.FILE_CANNOT_BE_EMPTY);

        try {
            return uploadService.upload(file.getInputStream(), file.getOriginalFilename(), type);
        } catch (IOException e) {
            throw new ServiceException(Code.FILE_UPLOAD_FAIL);
        }
    }

    private void allValidate(MultipartFile... files) {
        if ( files == null )
            throw new HttpException(HttpStatus.BAD_REQUEST, Code.FILE_CANNOT_BE_EMPTY);
        Arrays.stream(files).forEach(this::isNotEmpty);
    }

    private void isNotEmpty(MultipartFile file) {
        if ( file == null || file.isEmpty() )
            throw new HttpException(HttpStatus.BAD_REQUEST, Code.FILE_CANNOT_BE_EMPTY);
    }

    /**
     * 公钥
     * @return
     */
    @GetMapping("/key/public")
    public Ret<String> publicKey() {
        return Ret.ok(AppUtils.getBean(AuthorizationConfigProperties.class).getPublicKey());
    }

}
